package service.tajaApp.view; 

import java.util.ArrayList;

import javax.inject.Inject;
import javax.inject.Named;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import service.domain.BoardVO;
import service.domain.EventVO;
import service.domain.GameVO;
import service.domain.RankVO;
import service.domain.UserVO;
import service.tajaApp.TajaAppService;

@Controller
public class TajaAppController {
	
	@Inject
	@Named("tajaAppService")
	private TajaAppService tajaAppService;

	public void setTajaAppService(TajaAppService tajaAppService) {
		this.tajaAppService = tajaAppService;
	}

	public TajaAppController() { ; }

	@RequestMapping("/circular/tajaAppEvent.do")
	public ModelAndView tajaAppEvent(HttpServletRequest request) throws Exception {
		request.setCharacterEncoding("UTF-8");
		return new ModelAndView("/circular/event.jsp", "eventList", (ArrayList<EventVO>)tajaAppService.getEventList());		
	}
	
	@RequestMapping("/newsticker/tajaAppNews.do")
	public ModelAndView tajaAppNews(HttpServletRequest request) throws Exception {
		request.setCharacterEncoding("UTF-8");
		return new ModelAndView("/newsticker/news.jsp", "boardList", (ArrayList<BoardVO>)tajaAppService.getBoardList());
	}
	
	@RequestMapping("userInfo.do")
	public ModelAndView userInsert(@RequestParam(value="userId") String userId, 
			@RequestParam(value="userEmail") String email, 
			HttpServletRequest request) throws Exception {
		request.setCharacterEncoding("UTF-8");
		UserVO userVO = tajaAppService.getUserInfo(userId);
		if (userVO == null) {
			userVO = new UserVO(userId, email);
			tajaAppService.addUserInfo(userVO);
		}
		return new ModelAndView();	
	}
	
	@RequestMapping("getGameInfo.do")
	public ModelAndView gameInfoInsert(@RequestParam(value="userId") String userId,
			HttpServletRequest request) throws Exception {
		request.setCharacterEncoding("UTF-8");
		GameVO gameVO = tajaAppService.getGameInfo(userId); 
		if(gameVO==null) {
			tajaAppService.addGameInfo(userId);
			gameVO = tajaAppService.getGameInfo(userId);
		}
		return new ModelAndView("/getGameInfo.jsp", "gameVO", gameVO);	
	}
	
	@RequestMapping("updateGameInfo.do")
	public ModelAndView updategameInfo(@RequestParam(value="userId") String userId, 
			@RequestParam(value="gameName") String gameName, 
			@RequestParam(value="gameTime") String gameTime,
			HttpServletRequest request) throws Exception {
		request.setCharacterEncoding("UTF-8");
		tajaAppService.updateGameInfo(userId, gameName, Long.parseLong(gameTime));
		return new ModelAndView();
	}
		
	@RequestMapping("getRankList.do")
	public ModelAndView getRankList(HttpServletRequest request) throws Exception {
		request.setCharacterEncoding("UTF-8");
		ArrayList<RankVO> list = (ArrayList<RankVO>)tajaAppService.getRankList();
		request.setAttribute("size", list.size()-1);
		return new ModelAndView("/getRankList.jsp", "rankList", list);
	}
} // end class